<?php
/* --------------------------------------------------------------
   PropertiesAdminView.php 2009-12-14 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2009 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


class PropertiesAdminView 
{
	var $v_coo_properties_admin_control = false;
	
	var $m_env_get_array = array();
	var $m_env_post_array = array();
	
	var $m_content_array = array();
	var $m_content_template = '';
	
	function PropertiesAdminView($p_get_array, $p_post_array) 
	{
		$this->v_coo_properties_admin_control = new PropertiesAdminControl();
		
		$this->m_env_get_array 	= $p_get_array;
		$this->m_env_post_array = $p_post_array;
	}
	
	function proceed()
	{
		$t_output = '';
		if(isset($this->m_env_get_array['action']) == false) trigger_error('missing action', E_USER_ERROR);
		
		switch($this->m_env_get_array['action'])
		{
			case 'save':
				$t_properties_id = $this->v_coo_properties_admin_control->save(
																			$this->m_env_get_array['type'], 
																			$this->m_env_post_array);
				$t_output = (int)$t_properties_id;
				break;

			case 'delete':
				$t_properties_id = $this->v_coo_properties_admin_control->delete(
																			$this->m_env_get_array['type'], 
																			$this->m_env_get_array);
				$t_output = '';
				break;

			case 'load':
				switch($this->m_env_get_array['type'])
				{
					case 'properties_edit_form':
						if(isset($this->m_env_get_array['properties_id'])) {
							# update_form
							$t_output = $this->get_properties_edit($this->m_env_get_array['properties_id']);
						}
						else {
							# create_form
							$t_output = $this->get_properties_edit();
						}
						break;
					
					case 'properties_table':
						if(isset($this->m_env_get_array['properties_id'])) $c_properties_id = (int)$this->m_env_get_array['properties_id']; else trigger_error('missing properties_id', E_USER_ERROR);
						if(isset($this->m_env_get_array['language_id'])) $c_language_id = (int)$this->m_env_get_array['language_id']; else $c_language_id = false;
						
						$t_output = $this->get_properties_table($c_properties_id, $c_language_id);
						break;
						
					default:
						trigger_error('unknown type: '. $this->m_env_get_array['type'], E_USER_ERROR);
				}
				break;
				
			default:
				trigger_error('unknown action: '. $this->m_env_get_array['action'], E_USER_ERROR);
		}
		return $t_output;
	}
	
	function get_properties_edit($p_properties_id=false)
	{
		$c_properties_id = (int)$p_properties_id;
		
		if($c_properties_id > 0)
		{
			# load properties data by optional given properties_id
			$t_properties_names_array = $this->v_coo_properties_admin_control->get_properties_full_struct($c_properties_id, false, false);
			$t_sort_order = $t_properties_names_array['sort_order'];
			$t_properties_id = $t_properties_names_array['properties_id'];
		}
		else
		{
			# no properties_id given. use defaults and empty array for empty fields
			$t_properties_names_array = array();
			$t_sort_order = '1';
			$t_properties_id = '';
		}

		$t_languages_array = xtc_get_languages();
		$t_properties_names_fields_array = array();
		
		# merge language_array with (maybe empty) names_array
		for($i=0; $i<sizeof($t_languages_array); $i++)
		{
			if(isset($t_properties_names_array['PROPERTIES_NAMES'][$i]))
			{
				$t_value = $t_properties_names_array['PROPERTIES_NAMES'][$i];
			}
			else
			{
				$t_value = '';
			}
			$t_properties_names_fields_array[] = array_merge(
														$t_languages_array[$i],
														array('properties_name' => $t_value)
													);
		}
		
		# html build-data
		$t_content_data_array = array(
									'PROPERTIES_NAMES_FIELDS' => $t_properties_names_fields_array,
									'sort_order' => $t_sort_order,
									'properties_id' => $t_properties_id
								);
		$t_content_template = 'properties_edit_form.html';
		
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}
	
	function get_properties_main()
	{
		$t_properties_id_array = $this->v_coo_properties_admin_control->get_all_properties_ids();
		$t_properties_tables_array = array();
		
		foreach($t_properties_id_array as $t_properties_id)
		{
			$t_properties_tables_array[] = array(
												'properties_id' => $t_properties_id,
												'table_html'	=> $this->get_properties_table($t_properties_id)
											);
		}
		
		$t_content_data_array = array(
									'PROPERTIES_TABLES' => $t_properties_tables_array
								);
		$t_content_template = 'properties_main.html';
		
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}
	
	
	function get_properties_table($p_properties_id, $p_language_id=false)
	{
		$c_properties_id = (int)$p_properties_id;
		if($p_language_id !== false) $c_language_id = (int)$p_language_id; else $c_language_id = false;
		
		#get data array for assigning in smarty template
		$t_content_data_array = $this->v_coo_properties_admin_control->get_properties_full_struct($c_properties_id, $c_language_id);
		$t_content_template = 'properties_table.html';
		
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}

	
	
	
	function build_html($p_content_data_array, $p_template_file, $p_add_languages=true)
	{
		#language array for assigning in smarty template
		if($p_add_languages)
		{
			$t_languages_array = xtc_get_languages();
			$p_content_data_array = array_merge(
										array('LANGUAGES' => $t_languages_array), 
										$p_content_data_array);
		}
		$coo_smarty = new Smarty();
		
		# assign content
		$coo_smarty->assign('content_data', $p_content_data_array);
		
		# compile settings
		$coo_smarty->caching = false;
		$coo_smarty->compile_dir = DIR_FS_CATALOG.'templates_c/';
		
		# get html content
		$t_html_output = $coo_smarty->fetch(DIR_FS_CATALOG.'admin/templates/properties/'.$p_template_file);
		return $t_html_output;
	}
	
	function get_output() 
	{
		$t_content_array = $this->m_content_array;
		$t_template_file = $this->m_content_template;
		
		$t_html_output = $this->build_html($t_content_array, $t_template_file); 
		return $t_html_output;
	}
}















?>